package wx.controls
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import wx.core.Control;
	
	/**
	 * 
	 * @author wujianqi
	 * 按钮基类
	 * 
	 */	
	internal class ButtonBase extends Control
	{
		
		/**
		 *弹起及正常按钮皮肤 
		 */		
		private var _upSkin:DisplayObject;
		
		/**
		 * 停留按钮皮肤
		 */		
		private var _hoverSkin:DisplayObject;
		
		/**
		 *按下按钮皮肤 
		 */		
		private var _downSkin:DisplayObject;
		
		/**
		 *禁用按钮皮肤 
		 */		
		private var _disabledSkin:DisplayObject;
		
		/**
		 *当前皮肤状态 
		 */		
		private var _currentState:String = "up";
		
		private var _skinDisplay:DisplayObject;
		
		public function ButtonBase(up:DisplayObject,
								   hover:DisplayObject,
								   down:DisplayObject,
								   disabled:DisplayObject)
		{
			super();
			mouseChildren = false;
			
			_upSkin = up;
			_hoverSkin = hover;
			_downSkin = down;
			_disabledSkin = disabled;
			
			validSkinItem(_upSkin);
			validSkinItem(_hoverSkin);
			validSkinItem(_downSkin);
			validSkinItem(_disabledSkin);
			
			addSkin();
			
			if(_enable) bindEvent();
		}
		
		private function onMouseUp(e:MouseEvent):void{
			if(_currentState != "up"){
				_currentState = "up";
				updateState();
			}
		}
		private function onMouseDown(e:MouseEvent):void{
			e.stopPropagation();
			if(_currentState != "down"){
				_currentState = "down";
				updateState();
			}
		}
		private function onMouseOver(e:MouseEvent):void{
			if(_currentState != "hover"){
				_currentState = "hover";
				updateState();
			}
		}		
		
		override public function set enable(value:Boolean):void{
			if(_enable != value){
				_enable = value;
				_currentState = "disabled";
				if(!enable) unbindEvent();
				else bindEvent();
				updateState();
			}
		}
		
		
		private function bindEvent():void{
			addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			addEventListener(MouseEvent.ROLL_OVER,onMouseOver);
			addEventListener(MouseEvent.ROLL_OUT,onMouseUp);
		}
		
		private function unbindEvent():void{
			removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			removeEventListener(MouseEvent.ROLL_OVER,onMouseOver);
			removeEventListener(MouseEvent.ROLL_OUT,onMouseUp);
		}	
		
		public function set upSkin(value:DisplayObject):void
		{
			if(_upSkin != value){
				_upSkin = value;
				if(_currentState=="up") updateState();
			}
		}
		public function set hoverSkin(value:DisplayObject):void
		{
			if(_hoverSkin!= value){
				_hoverSkin = value;
				if(_currentState=="hover") updateState();
			}
			
		}
		public function set downSkin(value:DisplayObject):void
		{
			if(_downSkin != value){
				_downSkin = value;
				if(_currentState=="down") updateState();
			}
			
		}
		public function set disabledSkin(value:DisplayObject):void
		{
			if(_disabledSkin != value){
				_disabledSkin = value;
				if(_currentState=="disabled") updateState();
			}
			
		}
	
		/**
		 *添加皮肤层 
		 * 
		 */		
		private function addSkin():void{
			switch(_currentState)
			{
				case "up":
				{
					_skinDisplay =_upSkin;
					break;
				}
				case "down":
				{
					_skinDisplay =_downSkin;
					break;
				}
				case "hover":
				{
					_skinDisplay =_hoverSkin;
					break;
				}
				case "disabled":
				{
					_skinDisplay =_disabledSkin;
					break;
				}
			}			
			addChildAt(_skinDisplay,0);
		}
		
		/**
		 *组件布局绘制 
		 * 
		 */	
		protected function updateState():void{
			if(_skinDisplay!=null && contains(_skinDisplay)) removeChild(_skinDisplay);
			addSkin();
		}
		
		override protected function dispose():void{
			unbindEvent();
		}		
		
	}
}